#ifndef EPUBZLIB_COMM_FORMAT_HPP
#define EPUBZLIB_COMM_FORMAT_HPP

SJR_NAMESPACE_BEGIN

class EpubFormat final {
public:
  constexpr static int BUFFER_SIZE = 1024 * 32;

  template<class... Args>
  static auto format(std::string const &format, Args &&...args) -> std::string {
    return EpubFormat::instance().toFormat(format, args...);
  }

private:
  char mFmtBuffer[BUFFER_SIZE];
  EpubMutex mFmtSync;

  EpubFormat() : mFmtBuffer(), mFmtSync() {}

  static auto instance() -> EpubFormat & {
    static auto obj = (EpubFormat *) (nullptr);
    if (obj == nullptr)
      obj = new EpubFormat();
    return (*obj);
  }

  template<class... Args>
  auto toFormat(std::string const &format, Args &&...args) -> std::string {
    EpubUniqueLock lock(mFmtSync);
    snprintf(mFmtBuffer, sizeof(mFmtBuffer), format.c_str(), args...);
    return mFmtBuffer;
  }
};

SJR_NAMESPACE_END

#endif//EPUBZLIB_COMM_FORMAT_HPP
